package controllers;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import dao.CategoryDAORemote;
import dto.CategoryDTO;
import dto.DomainDTO;

@ManagedBean
@SessionScoped
public class CategoryController {

	@EJB
	CategoryDAORemote categoryDAO;

	@ManagedProperty(value = "#{categoryDTO}")
	CategoryDTO categoryDTO;

	private List<CategoryDTO> listOfCategories;
	private List<String> checkbox = new ArrayList<String>();
	private int currentDomainId;
	private String messageClass;

	@PostConstruct
	public void setCurrentDID() {
		currentDomainId = -1;
	}

	public CategoryDTO getCategoryDTO() {
		return categoryDTO;
	}

	public void setCategoryDTO(CategoryDTO categoryDTO) {
		this.categoryDTO = categoryDTO;
	}

	public List<String> getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(List<String> checkbox) {
		this.checkbox.addAll(checkbox);
	}

	public void setCurrentDomainId(int currentDomainId) {
		listOfCategories = categoryDAO.getCategoryByDomain(currentDomainId);
		this.currentDomainId = currentDomainId;
	}

	public int getCurrentDomainId() {
		return currentDomainId;
	}

	public List<CategoryDTO> getListOfCategories() {
		return listOfCategories;
	}

	public void setListOfCategories(List<CategoryDTO> listOfCategories) {
		this.listOfCategories = listOfCategories;
	}

	public void addCategory(ActionEvent event) {
		try {

			if (categoryDTO.getName() == "" || categoryDTO.getName() == null) {
				FacesContext context = FacesContext.getCurrentInstance();
				context.addMessage(event.getComponent().getClientId(), new FacesMessage("Please, type a category!"));
				messageClass = "errorMessage";
			} else if (categoryDAO.getCategoryByNameAndDomain(categoryDTO.getName(), currentDomainId) != null) {
				FacesContext context = FacesContext.getCurrentInstance();
				context
						.addMessage(event.getComponent().getClientId(),
								new FacesMessage("The category already exists!"));
				messageClass = "errorMessage";
			} else {
				DomainDTO d = new DomainDTO();
				d.setId(currentDomainId);
				categoryDAO.addCategory(categoryDTO, d);
				listOfCategories = categoryDAO.getCategoryByDomain(currentDomainId);
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		categoryDTO.setName("");
	}

	public void deleteCategories(ActionEvent event) {
		if (checkbox.size() != 0) {
			for (String s : checkbox) {
				CategoryDTO c = new CategoryDTO();
				c.setId(Integer.parseInt(s));
				try {
					categoryDAO.removeCategory(c);
				} catch (Exception e) {
					System.out.println(e.getMessage());
					e.printStackTrace();
				}
			}
			checkbox.clear();
			listOfCategories = categoryDAO.getCategoryByDomain(currentDomainId);
		} else {
			FacesContext context = FacesContext.getCurrentInstance();
			String clientId = event.getComponent().getClientId(context);
			context.addMessage(clientId, new FacesMessage("Please, select at least one category!"));
			messageClass = "errorMessage";
		}
	}

	/**
	 * @param messageClass
	 *            the messageClass to set
	 */
	public void setMessageClass(String messageClass) {
		this.messageClass = messageClass;
	}

	/**
	 * @return the messageClass
	 */
	public String getMessageClass() {
		return messageClass;
	}
}
